/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package data.xml_persistence.logic;

import data.EntityContainer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author ded_end
 */
public class DataTransformer {

    public static EntityContainer transformToObjects(data.xml_persistence.model.DataModel model) {

        data.xml_persistence.model.AirlineList airlineList = model.getAirlines();
        data.xml_persistence.model.AirplaneList airplaneList = model.getAirplanes();
        data.xml_persistence.model.AirportList airportList = model.getAirports();
        data.xml_persistence.model.FlightList flightList = model.getFlights();
        data.xml_persistence.model.PlaceList placeList = model.getPlaces();
        data.xml_persistence.model.RouteList routeList = model.getRoutes();

        List<data.xml_persistence.transform_model.Airline> airlines = new ArrayList<data.xml_persistence.transform_model.Airline>();
        List<data.xml_persistence.transform_model.Airplane> airplanes = new ArrayList<data.xml_persistence.transform_model.Airplane>();
        List<data.xml_persistence.transform_model.Airport> airports = new ArrayList<data.xml_persistence.transform_model.Airport>();
        List<data.xml_persistence.transform_model.Flight> flights = new ArrayList<data.xml_persistence.transform_model.Flight>();
        List<data.xml_persistence.transform_model.Place> places = new ArrayList<data.xml_persistence.transform_model.Place>();
        List<data.xml_persistence.transform_model.Route> routes = new ArrayList<data.xml_persistence.transform_model.Route>();

        /* transformace z xml_persistence.model do xml_persistence.transform_model */
        for (data.xml_persistence.model.Airline a : airlineList.getAirline()) {
            List<String> airplaneNames = new ArrayList<String>();
            for (data.xml_persistence.model.AirplaneName an : a.getAirplanes().getAirplane()) {
                airplaneNames.add(an.getId());
            }
            airlines.add(new data.xml_persistence.transform_model.Airline(a.getId(), a.getName(), a.getCountry(), a.getRating(), airplaneNames));
        }

        for (data.xml_persistence.model.Airplane a : airplaneList.getAirplane()) {
            airplanes.add(new data.xml_persistence.transform_model.Airplane(a.getId(), a.getModel(), a.getSeats(), a.getAvgConsumption(), a.getMaxConsumption(), a.getMinConsumption(), a.getCapacity()));
        }

        for (data.xml_persistence.model.Airport a : airportList.getAirport()) {
            airports.add(new data.xml_persistence.transform_model.Airport(a.getId(), a.getName(), a.getTaxes(), a.getAnnual_number_of_passengers(), a.getPlaceId().getId()));
        }

        for (data.xml_persistence.model.Flight a : flightList.getFlight()) {
            // ZMENA POZOR!!!!!
            String s = null;
            s = a.getDeparture_time();
            s = s.substring(0, s.indexOf('C')) + s.substring(s.indexOf('C') + 4);
            System.out.println("S " + s);

            String d = null;
            d = a.getArrival_time();
            d = d.substring(0, d.indexOf('C')) + d.substring(d.indexOf('C') + 4);
            System.out.println("D " + d);

            flights.add(new data.xml_persistence.transform_model.Flight(a.getId(), a.getCode(), new Date(s), new Date(d), a.getCost(), a.getLength(), a.getAirlineName().getId(), a.getAirplaneName().getId(), a.getRoute().getId()));
        }

        for (data.xml_persistence.model.Place a : placeList.getPlace()) {
            places.add(new data.xml_persistence.transform_model.Place(a.getId(), a.getName(), a.getImportance(), a.getPopulation()));
        }

        for (data.xml_persistence.model.Route a : routeList.getRoute()) {
            routes.add(new data.xml_persistence.transform_model.Route(a.getId(), a.getFrequency(), a.getRating(), a.getWeekly_passangers(), a.getDeparture().getId(), a.getArriving().getId()));
        }

        /* transformace z xml_persistence.transform_model do data.model */
        List<data.model.Airline> airlines_model = new ArrayList<data.model.Airline>();
        List<data.model.Airplane> airplanes_model = new ArrayList<data.model.Airplane>();
        List<data.model.Airport> airports_model = new ArrayList<data.model.Airport>();
        List<data.model.Flight> flights_model = new ArrayList<data.model.Flight>();
        List<data.model.Place> places_model = new ArrayList<data.model.Place>();
        List<data.model.Route> routes_model = new ArrayList<data.model.Route>();

        // airplanes
        for (data.xml_persistence.transform_model.Airplane a : airplanes) {
            airplanes_model.add(new data.model.Airplane(a.getId(), a.getModel(), a.getSeats(), a.getAvgConsumption(), a.getMaxConsumption(), a.getMinConsumption(), a.getCapacity()));
        }

        // places
        for (data.xml_persistence.transform_model.Place a : places) {
            places_model.add(new data.model.Place(a.getId(), a.getName(), a.getImportance(), a.getPopulation()));
        }

        // airlines
        for (data.xml_persistence.transform_model.Airline a : airlines) {
            List<data.model.Airplane> list = new ArrayList<data.model.Airplane>();
            for (String s : a.getAirplanes()) {
                for (data.model.Airplane ap : airplanes_model) {
                    if (ap.getId().equals(s)) {
                        list.add(ap);
                        break;
                    }
                }
            }
            airlines_model.add(new data.model.Airline(a.getId(), a.getName(), a.getCountry(), a.getRating(), list));
        }

        // airports
        for (data.xml_persistence.transform_model.Airport a : airports) {
            data.model.Place place = null;
            for (data.model.Place pl : places_model) {
                if (pl.getId().equals(a.getPlace())) {
                    place = pl;
                    break;
                }
            }
            airports_model.add(new data.model.Airport(a.getId(), a.getName(), a.getTaxes(), a.getAnnual_number_of_passengers(), place));
        }

        // routes
        for (data.xml_persistence.transform_model.Route r : routes) {
            data.model.Airport airport1 = null;
            data.model.Airport airport2 = null;
            for (data.model.Airport ar : airports_model) {
                if (ar.getId().equals(r.getDepartureAirport())) {
                    airport1 = ar;
                    break;
                }
            }
            for (data.model.Airport ar : airports_model) {
                if (ar.getId().equals(r.getArrivalAirport())) {
                    airport2 = ar;
                    break;
                }
            }
            routes_model.add(new data.model.Route(r.getId(), r.getFrequency(), r.getRating(), r.getWeeklyPassengers(), airport1, airport2));
        }

        // flights
        for (data.xml_persistence.transform_model.Flight a : flights) {
            data.model.Airline airline = null;
            data.model.Airplane airplane = null;
            data.model.Route route = null;

            for (data.model.Airline al : airlines_model) {
                if (al.getId().equals(a.getAirline())) {
                    airline = al;
                }
            }

            for (data.model.Airplane ap : airplanes_model) {
                if (ap.getId().equals(a.getAirplane())) {
                    airplane = ap;
                }
            }

            for (data.model.Route r : routes_model) {
                if (r.getId().equals(a.getRoute())) {
                    route = r;
                }
            }
            flights_model.add(new data.model.Flight(a.getId(), a.getCode(), a.getDepartureTime(), a.getArrivalTime(), a.getCost(), a.getLenght(), airline, airplane, route));
        }



        // EntityContainer
        data.EntityContainer con = new data.EntityContainer();
        con.setAirlines(airlines_model);
        con.setAirplanes(airplanes_model);
        con.setAirports(airports_model);
        con.setFlights(flights_model);
        con.setPlaces(places_model);
        con.setRoutes(routes_model);

        return con;
    }

    public static data.xml_persistence.model.DataModel transformToXML(EntityContainer con) {


        // inicializace seznamu pro uchovani transformovanych objektu z EntityContainer do data.xml_persistence.transform_model
        List<data.xml_persistence.transform_model.Airline> airlines = new ArrayList<data.xml_persistence.transform_model.Airline>();
        List<data.xml_persistence.transform_model.Airplane> airplanes = new ArrayList<data.xml_persistence.transform_model.Airplane>();
        List<data.xml_persistence.transform_model.Airport> airports = new ArrayList<data.xml_persistence.transform_model.Airport>();
        List<data.xml_persistence.transform_model.Flight> flights = new ArrayList<data.xml_persistence.transform_model.Flight>();
        List<data.xml_persistence.transform_model.Place> places = new ArrayList<data.xml_persistence.transform_model.Place>();
        List<data.xml_persistence.transform_model.Route> routes = new ArrayList<data.xml_persistence.transform_model.Route>();

        // transformace objektu z EntityContainer do data.xml_persistence.transform_model
        // Airlines
        for (data.model.Airline al : con.getAirlines()) {
            List<String> list = new ArrayList<String>();
            for (data.model.Airplane ap : al.getAirplanes()) {
                list.add(ap.getId());
            }
            airlines.add(new data.xml_persistence.transform_model.Airline(al.getId(), al.getName(), al.getCountry(), al.getRating(), list));
        }

        // Airplanes
        for (data.model.Airplane ap : con.getAirplanes()) {
            airplanes.add(new data.xml_persistence.transform_model.Airplane(ap.getId(), ap.getModel(), ap.getSeats(), ap.getAvgConsumption(), ap.getMaxConsumption(), ap.getMinConsumption(), ap.getCapacity()));
        }

        // Airports
        for (data.model.Airport ap : con.getAirports()) {
            airports.add(new data.xml_persistence.transform_model.Airport(ap.getId(), ap.getName(), ap.getTaxes(), ap.getAnnual_number_of_passengers(), ap.getPlace().getId()));
        }

        // Flights
        for (data.model.Flight f : con.getFlights()) {
            String airline = f.getAirline().getId();
            String airplane = f.getAirplane().getId();
            String route = f.getRoute().getId();
            flights.add(new data.xml_persistence.transform_model.Flight(f.getId(), f.getCode(), f.getDepartureTime(), f.getArrivalTime(), f.getCost(), f.getLenght(), airline, airplane, route));
        }

        // Places
        for (data.model.Place p : con.getPlaces()) {
            places.add(new data.xml_persistence.transform_model.Place(p.getId(), p.getName(), p.getImportance(), p.getPopulation()));
        }

        // Routes
        for (data.model.Route r : con.getRoutes()) {
            String airport1 = r.getDepartureAirport().getId();
            String airport2 = r.getArrivalAirport().getId();
            routes.add(new data.xml_persistence.transform_model.Route(r.getId(), r.getFrequency(), r.getRating(), r.getWeeklyPassengers(), airport1, airport2));
        }

        // transformace objektu z data.xml_persistence.transform_model do data.xml_persistence.model       
        // inicializace seznamu pro uchovani transformovanych objektu z data.xml_persistence.transform_model do data.xml_persisetence.model
        List<data.xml_persistence.model.Airline> airlineList = new ArrayList<data.xml_persistence.model.Airline>();
        List<data.xml_persistence.model.Airplane> airplaneList = new ArrayList<data.xml_persistence.model.Airplane>();
        List<data.xml_persistence.model.Airport> airportList = new ArrayList<data.xml_persistence.model.Airport>();
        List<data.xml_persistence.model.Flight> flightList = new ArrayList<data.xml_persistence.model.Flight>();
        List<data.xml_persistence.model.Place> placeList = new ArrayList<data.xml_persistence.model.Place>();
        List<data.xml_persistence.model.Route> routeList = new ArrayList<data.xml_persistence.model.Route>();

        // airlineList
        for (data.xml_persistence.transform_model.Airline a : airlines) {
            ArrayList<data.xml_persistence.model.AirplaneName> anl = new ArrayList<data.xml_persistence.model.AirplaneName>();
            for (String s : a.getAirplanes()) {
                data.xml_persistence.model.AirplaneName an = new data.xml_persistence.model.AirplaneName();
                an.setId(s);
                anl.add(an);
            }
            data.xml_persistence.model.AirplaneNameList airplaneNameList = new data.xml_persistence.model.AirplaneNameList();
            airplaneNameList.setAirplane(anl);
            airlineList.add(new data.xml_persistence.model.Airline(a.getId(), a.getName(), a.getCountry(), a.getRating(), airplaneNameList));
        }

        // airplaneList
        for (data.xml_persistence.transform_model.Airplane a : airplanes) {
            airplaneList.add(new data.xml_persistence.model.Airplane(a.getId(), a.getModel(), a.getSeats(), a.getAvgConsumption(), a.getMaxConsumption(), a.getMinConsumption(), a.getCapacity()));
        }

        // airportList
        for (data.xml_persistence.transform_model.Airport a : airports) {
            data.xml_persistence.model.PlaceId place_id = new data.xml_persistence.model.PlaceId();
            place_id.setId(a.getPlace());
            airportList.add(new data.xml_persistence.model.Airport(a.getId(), a.getName(), a.getTaxes(), a.getAnnual_number_of_passengers(), place_id));
        }

        // flightList
        for (data.xml_persistence.transform_model.Flight a : flights) {
            data.xml_persistence.model.AirlineName airline_name = new data.xml_persistence.model.AirlineName();
            data.xml_persistence.model.AirplaneName airplane_name = new data.xml_persistence.model.AirplaneName();
            data.xml_persistence.model.RouteName route_name = new data.xml_persistence.model.RouteName();

            airline_name.setId(a.getAirline());
            airplane_name.setId(a.getAirplane());
            route_name.setId(a.getRoute());
            flightList.add(new data.xml_persistence.model.Flight(a.getId(), a.getCode(), a.getDepartureTime().toString(), a.getArrivalTime().toString(), a.getCost(), a.getLenght(), airline_name, airplane_name, route_name));
        }

        // placeList
        for (data.xml_persistence.transform_model.Place a : places) {
            placeList.add(new data.xml_persistence.model.Place(a.getId(), a.getName(), a.getImportance(), a.getPopulation()));
        }

        // routeList
        for (data.xml_persistence.transform_model.Route a : routes) {
            data.xml_persistence.model.AirportName airport1 = new data.xml_persistence.model.AirportName();
            data.xml_persistence.model.AirportName airport2 = new data.xml_persistence.model.AirportName();

            airport1.setId(a.getDepartureAirport());
            airport2.setId(a.getArrivalAirport());

            routeList.add(new data.xml_persistence.model.Route(a.getId(), a.getFrequency(), (int) a.getRating(), a.getWeeklyPassengers(), airport1, airport2));
        }


        data.xml_persistence.model.DataModel dataModel = new data.xml_persistence.model.DataModel();

        // vlozeni seznamu do listu
        data.xml_persistence.model.AirlineList list1 = new data.xml_persistence.model.AirlineList();
        data.xml_persistence.model.AirplaneList list2 = new data.xml_persistence.model.AirplaneList();
        data.xml_persistence.model.AirportList list3 = new data.xml_persistence.model.AirportList();
        data.xml_persistence.model.FlightList list4 = new data.xml_persistence.model.FlightList();
        data.xml_persistence.model.PlaceList list5 = new data.xml_persistence.model.PlaceList();
        data.xml_persistence.model.RouteList list6 = new data.xml_persistence.model.RouteList();

        list1.setAirline(airlineList);
        list2.setAirplane(airplaneList);
        list3.setAirport(airportList);
        list4.setFlight(flightList);
        list5.setPlace(placeList);
        list6.setRoute(routeList);

        dataModel.setAirlines(list1);
        dataModel.setAirplanes(list2);
        dataModel.setAirports(list3);
        dataModel.setFlights(list4);
        dataModel.setPlaces(list5);
        dataModel.setRoutes(list6);

        return dataModel;
    }
}
